package com.dries.portal.module.user.service.impl;

import cn.hutool.json.JSONUtil;
import com.dries.common.constant.Constant;
import com.dries.portal.module.user.mapper.UserMapper;
import com.dries.portal.module.user.entity.UserEntity;
import com.dries.portal.module.user.service.UserCacheService;
import com.dries.security.annotation.CacheException;
import com.dries.security.service.RedisService;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;

/**
 * @Description:
 * @Author ZSY
 * @createTime 2020/6/29 9:14
 */
@Service
@Slf4j
public class UserCacheServiceImpl implements UserCacheService {

    @Resource
    private RedisService redisService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private UserMapper userMapper;
    @Value("${redis.database}")
    private String REDIS_DATABASE;
    @Value("${redis.expire.common}")
    private Long REDIS_EXPIRE;
    @Value("${redis.expire.authCode}")
    private Long REDIS_EXPIRE_AUTH_CODE;
    @Value("${redis.key.user}")
    private String REDIS_KEY_USER;
    @Value("${redis.key.authCode}")
    private String REDIS_KEY_AUTH_CODE;

    private Long AUTH_CODE_EXPIRE_SECONDS;

    /**
     * 删除会员用户缓存
     *
     * @param userId
     */
    @Override
    public void delUser(Long userId) {
        UserEntity user = userMapper.selectById(userId);

        if (user != null) {
            String key = REDIS_DATABASE + ":" + REDIS_KEY_USER + ":" + user.getUsername();
            redisService.del(key);
            redisService.del(user.getOpenid());
        }
    }

    /**
     * 获取会员用户缓存
     *
     * @param username
     */
    @Override
    public UserEntity getUser(String username) {

        String key = REDIS_DATABASE + ":" + REDIS_KEY_USER + ":" + username;

        return (UserEntity) redisService.get(key);
    }

    /**
     * 获取会员用户缓存
     *
     * @param openId
     */
    @Override
    public UserEntity getUserByOpenId(String openId) {

        return (UserEntity) redisService.get(openId);
    }


    /**
     * 设置会员用户缓存
     *
     * @param user
     */
    @Override
    public void setUser(UserEntity user) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_USER + ":" + user.getUsername();
        redisService.set(key, user, REDIS_EXPIRE);
    }

    /**
     * 设置会员用户缓存
     *
     * @param user
     */
    @Override
    public void setUserByOpenId(UserEntity user) {
        String key = user.getOpenid();
        redisService.set(key, user, REDIS_EXPIRE);
    }

    @Override
    public void setUserChannelRel(String sendUserId, Map<String, Channel> manager) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_USER + ":" + sendUserId;
        redisService.set(key, JSONUtil.parse(manager));
//        redisTemplate.opsForHash().putAll(key,  JSONUtil.parse(manager));
    }

    @Override
    public Channel getUserChannelRel(String sendUserId) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_USER + ":" + sendUserId;
        Map<String, Channel> entries = redisTemplate.opsForHash().entries(key);
        log.info("======================>" + entries.get(key));
        return entries.get(sendUserId);
    }

    /**
     * 设置优惠券
     */
    @Override
    public void setCouponCode(Integer id, Set<String> couponCodes) {
        String key = String.format("%s%s", Constant.RedisPrefix.COUPON_TEMPLATE, id.toString());
        redisTemplate.opsForList().rightPushAll(key, couponCodes);
    }

    /**
     * 设置验证码
     *
     * @param telephone
     * @param authCode
     */
    @CacheException
    @Override
    public void setAuthCode(String telephone, String authCode) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_AUTH_CODE + ":" + telephone;
        redisService.set(key,authCode,REDIS_EXPIRE_AUTH_CODE);
    }

    /**
     * 获取验证码
     *
     * @param telephone
     */
    @CacheException
    @Override
    public String getAuthCode(String telephone) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_AUTH_CODE + ":" + telephone;
        return (String) redisService.get(key);
    }
}
